package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.MmrjT;
import cn.e4j.modular.crm.entity.MmrjlT;
import cn.e4j.modular.crm.mapper.MmrjTMapper;
import cn.e4j.modular.crm.service.MmrjTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 标准评价指标维护 服务类
 * @author mengyb
 * @date 2019-04-16
 */
@Service
public class MmrjTServiceImpl extends ServiceImpl<MmrjTMapper, MmrjT> implements MmrjTService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser= ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);
    }
    @Transactional
    @Override
    public void add(MmrjT mmrjT, MmrjlT mmrjlT) {
        ShiroUser shiroUser= ShiroKit.getUser();

        mmrjT.setMMRJENT(shiroUser.getDBC());
        mmrjT.setMMRJUSER(shiroUser.getACCOUNT());
        mmrjT.setMMRJGRUP(shiroUser.getORG_ID());
        mmrjT.setMMRJORIU(shiroUser.getACCOUNT());
        mmrjT.setMMRJORIG(shiroUser.getORG_ID());
        mmrjT.setMMRJTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        mmrjT.setMMRJDATE(LocalDate.now());

        mmrjlT.setMMRJLENT(shiroUser.getDBC());
        mmrjlT.setMMRJL001(mmrjT.getMMRJ001());
        mmrjlT.setMMRJL002(mmrjT.getMMRJ002());
        mmrjlT.setMMRJL003(LangUtils.getLang(shiroUser.getLANG()));

        mmrjT.insert();
        mmrjlT.insert();

    }
    @Transactional
    @Override
    public void edit(MmrjT mmrjT, MmrjlT mmrjlT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        mmrjT.setMMRJMODU(shiroUser.getACCOUNT());

        UpdateWrapper<MmrjT> mmrjTUpdateWrapper = new UpdateWrapper<>();

        mmrjTUpdateWrapper.eq("MMRJENT",mmrjT.getMMRJENT());
        mmrjTUpdateWrapper.eq("MMRJ001", mmrjT.getMMRJ001());

        UpdateWrapper<MmrjlT> mmrjlTUpdateWrapper=new UpdateWrapper<>();

        mmrjlTUpdateWrapper.eq("MMRJLENT",mmrjT.getMMRJENT());
        mmrjlTUpdateWrapper.eq("MMRJL001",mmrjT.getMMRJ001());

        mmrjT.update(mmrjTUpdateWrapper);
        mmrjlT.update(mmrjlTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRow(MmrjT mmrjT, MmrjlT mmrjlT) {
        UpdateWrapper<MmrjT> mmrjTUpdateWrapper = new UpdateWrapper<>();

        mmrjTUpdateWrapper.eq("MMRJENT",mmrjT.getMMRJENT());
        mmrjTUpdateWrapper.eq("MMRJ001", mmrjT.getMMRJ001());

        UpdateWrapper<MmrjlT> mmrjlTUpdateWrapper=new UpdateWrapper<>();

        mmrjlTUpdateWrapper.eq("MMRJLENT",mmrjT.getMMRJENT());
        mmrjlTUpdateWrapper.eq("MMRJL001",mmrjT.getMMRJ001());

        mmrjT.delete(mmrjTUpdateWrapper);
        mmrjlT.delete(mmrjlTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRows(List<MmrjT> mmrjTList) {
        for (MmrjT mmrjT : mmrjTList
        ) {
            UpdateWrapper<MmrjT> mmrjTUpdateWrapper = new UpdateWrapper<>();

            mmrjTUpdateWrapper.eq("MMRJENT",mmrjT.getMMRJENT());
            mmrjTUpdateWrapper.eq("MMRJ001", mmrjT.getMMRJ001());


            MmrjlT mmrjlT=new MmrjlT();
            UpdateWrapper<MmrjlT> mmrjlTUpdateWrapper=new UpdateWrapper<>();

            mmrjlTUpdateWrapper.eq("MMRJLENT",mmrjT.getMMRJENT());
            mmrjlTUpdateWrapper.eq("MMRJL001",mmrjT.getMMRJ001());

            mmrjT.delete(mmrjTUpdateWrapper);
            mmrjlT.delete(mmrjlTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(MmrjT mmrjT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        mmrjT.setMMRJENT(shiroUser.getDBC());
        QueryWrapper<MmrjT> mmrjTQueryWrapper = new QueryWrapper<>();
        mmrjTQueryWrapper.eq("MMRJENT", mmrjT.getMMRJENT());
        mmrjTQueryWrapper.eq("MMRJ001", mmrjT.getMMRJ001());
        return this.baseMapper.selectCount(mmrjTQueryWrapper)>0?true:false;
    }
}
